import axios from 'axios';
import request from '@/utils/request';
import type { Result } from '@/types/global';

export interface CarGroupLabelConfig {
  id?: number;
  labelName: string;
  labelValue: string;
  isDelete: number;
}

export interface PartnerPropVo {
  partnerId: number;
  partnerName?: string;
  percent: number;
}

export interface CarBillingIntervalVo {
  startTime: string;
  endTime: string;
  price: number;
}

export interface CarBillingMethodVo {
  price: number;
}

export interface CarChargeSettingVo {
  elecPriceSetting: CarBillingMethodVo;
  intervalSettingList: CarBillingIntervalVo[];
}

export interface CarChargingGroupDetailVo {
  id?: number;
  name: string;
  address: string;
  areaCode?: string;
  chargingGroupType: number;
  chargingGroupLat: string;
  changingGroupLon: string;
  partnerProps?: PartnerPropVo[];
  siteImages?: string;
  siteDescription?: string;
  parkDescription?: string;
  electricPrice?: string;
  openingHoursBegin?: string;
  openingHoursEnd?: string;
  createDate?: string;
  carGroupLabels?: CarGroupLabelConfig[];
  servicePhone?: string;
  propType?: number;
  carBillingMethod?: CarChargeSettingVo;
  province?: string;
  city?: string;
  district?: string;
}

export interface CarChargingGroupQueryVo {
  current: number;
  size: number;
  name?: string;
  areaCode?: string;
  chargingGroupType?: number;
  partnerId?: number;
}

export interface AddCarChargingGroupReq {
  name: string;
  address: string;
  areaCode?: string;
  chargingGroupType: number;
  chargingGroupLat: string;
  changingGroupLon: string;
  partnerProps?: PartnerPropVo[];
  siteImages?: string;
  siteDescription?: string;
  parkDescription?: string;
  electricPrice?: string;
  openingHoursBegin?: string;
  openingHoursEnd?: string;
  carGroupLabels?: CarGroupLabelConfig[];
  servicePhone?: string;
  propType?: number;
  carBillingMethod?: CarChargeSettingVo;
  province?: string;
  city?: string;
  district?: string;
}

// 获取站点标签
export function getCarChargingGroupLabelList() {
  return axios.get('/api/v1/car/charging/group/label/list');
}

// 分页查询汽车充电站点列表
export function listCarChargingGroups(params: CarChargingGroupQueryVo) {
  return axios.get('/api/v1/car/charging/group/list', {
    params
  });
}

// 获取汽车站点信息
export function getCarChargingGroupInfo(id: number) {
  return axios.get('/api/v1/car/charging/group/info', {
    params: { id }
  });
}

// 获取站点下所有设备二维码
export function getChargingGroupQrCodes(groupId: number) {
  return axios.get('/api/v1/car/charging/group/qrcodes', {
    params: { groupId }
  });
}

// 新增汽车站点
export function addCarChargingGroup(data: AddCarChargingGroupReq) {
  return axios.post('/api/v1/car/charging/group/add', data);
}

// 更新汽车站点
export function updateCarChargingGroup(data: CarChargingGroupDetailVo) {
  return axios.post('/api/v1/car/charging/group/update', data);
}

/**
 * 删除汽车充电站点
 * @param id 站点ID
 */
export function deleteCarChargingGroup(id: number) {
  return axios.post('/api/v1/car/charging/group/delete', null, {
    params: { id }
  });
}

// 获取所有汽车充电站点
export function getAllCarChargingGroups(params?: { partnerId?: number }): Promise<Result<{ id: number; name: string }[]>> {
  return request.get('/api/v1/car/charging/group/all', params);
}

// 充电桩二维码数据
export interface ChargingQrCodeVo {
  chargingId: number;
  chargingName: string;
  chargingCode: string;
  qrCode: string;
}

// 充电桩及其二维码数据
export interface ChargingWithQrCodesVo {
  chargingId?: number;
  chargingName?: string;
  chargingCode?: string;
  qrCodes: ChargingQrCodeVo[];
} 